class FormatString {
  formatFloat(x, pos) {
    let f = parseFloat(x);
    if (isNaN(f)) {
      return 0;
    }
    let n = Math.pow(10, pos);
    let k = Math.round(x * n) / n;
    let s = k.toString();
    let rs = s.indexOf(".");
    if (rs < 0 && pos > 0) {
      rs = s.length;
      s += ".";
    }
    while (s.length <= rs + pos) {
      s += "0";
    }
    return s;
  }
  // 货币格式化
  C(data, pos) {
    return `￥${this.N(data, pos)}`;
  }
  // 逗号隔开 保留n位小数
  N(data, pos) {
    if (pos <= 0) {
      let floatNum = String(data).split(".")[1];
      if (floatNum) {
        pos = floatNum.length;
      } else {
        pos = 0;
      }
    }
    let strData = this.formatFloat(data, pos);
    let minusSign = "";
    if ((strData as number) < 0) {
      minusSign = "-";
    }
    let intNum = String(Math.abs(strData as number)).split(".")[0];
    if (intNum.length <= 3) {
      return strData;
    } else {
      let reverseNum = intNum.split("").reverse();
      let reverseNullNum = [];
      reverseNum.map((item, index) => {
        reverseNullNum.unshift(item);
        if ((index + 1) % 3 === 0) {
          if (index + 1 !== reverseNum.length) {
            reverseNullNum.unshift(",");
          }
        }
      });
      if (pos === 0) {
        return minusSign + reverseNullNum.join("");
      } else {
        return minusSign + reverseNullNum.join("") + "." + (strData as string).split(".")[1];
      }
    }
  }
  // 保留n位小数
  F(data, pos) {
    return this.formatFloat(data, pos);
  }
  // 百分比格式化
  P(data, pos) {
    return this.formatFloat(data * 100, pos) + "%";
  }

  // 数字格式化
  formatNumber(format, data) {
    let pos = format.substr(1, format.length);
    if (pos) {
      pos = Number(pos);
    } else {
      pos = 2;
    }
    let FormatFn = format.substr(0, 1);
    let returnData = this[FormatFn](data, pos);
    if (returnData.includes && returnData.includes("NaN")) {
      return "0";
    } else {
      return returnData;
    }
  }
  // 金额转大写
  convertCurrency(formatStr, money) {
    //汉字的数字
    const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
    //基本单位
    const cnIntRadice = ["", "拾", "佰", "仟"];
    //对应整数部分扩展单位
    const cnIntUnits = ["", "万", "亿", "兆"];
    //对应小数部分单位
    const cnDecUnits = ["角", "分", "毫", "厘"];
    //整数金额时后面跟的字符
    const cnInteger = "整";
    //整型完以后的单位
    const cnIntLast = "元";
    //最大处理的数字
    const maxNum = 999999999999999;
    //金额整数部分f
    let integerNum;
    //金额小数部分
    let decimalNum;
    //输出的中文金额字符串
    let chineseStr = "";
    //分离金额后用的数组，预定义
    let parts;
    if (money === "") {
      return "";
    }
    money = parseFloat(money);
    if (money >= maxNum) {
      //超出最大处理数字
      return "";
    }
    if (money === 0) {
      chineseStr = cnNums[0] + cnIntLast + cnInteger;
      return chineseStr;
    }
    //转换为字符串
    money = money.toString();
    if (money.indexOf(".") === -1) {
      integerNum = money;
      decimalNum = "";
    } else {
      parts = money.split(".");
      integerNum = parts[0];
      decimalNum = parts[1].substr(0, 4);
    }
    //获取整型部分转换
    if (parseInt(integerNum, 10) > 0) {
      let zeroCount = 0;
      let IntLen = integerNum.length;
      for (let i = 0; i < IntLen; i++) {
        let n = integerNum.substr(i, 1);
        let p = IntLen - i - 1;
        let q = p / 4;
        let m = p % 4;
        if (n === "0") {
          zeroCount++;
        } else {
          if (zeroCount > 0) {
            chineseStr += cnNums[0];
          }
          //归零
          zeroCount = 0;
          chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
        }
        if (m === 0 && zeroCount < 4) {
          chineseStr += cnIntUnits[q];
        }
      }
      chineseStr += cnIntLast;
    }
    //小数部分
    if (decimalNum !== "") {
      var decLen = decimalNum.length;
      for (var i = 0; i < decLen; i++) {
        var n = decimalNum.substr(i, 1);
        if (n !== "0") {
          chineseStr += cnNums[Number(n)] + cnDecUnits[i];
        }
      }
    }
    if (chineseStr === "") {
      chineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (decimalNum === "") {
      chineseStr += cnInteger;
    }
    return chineseStr;
  }
}
export const format = (dataType, formatStr, formatData) => {
  let formatFn = new FormatString();
  if (dataType === 3 || dataType === "3") {
    return formatFn.formatNumber(formatStr, formatData);
  } else if (dataType === 2 || dataType === "2") {
    return formatFn.convertCurrency(formatStr, formatData);
  }
};
